﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Data;
using KeaoBase;

namespace KeaoDataService
{
    public class AccessDB
    {
        private static OleDbConnection Conn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};{1}", KeaoUtils.ConfigGetNodeAttr("系统配置", "数据文件"), string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("系统配置", "数据密码")) ? "" : string.Format("Jet OleDb:DataBase Password='{0}';", KeaoUtils.ConfigGetNodeAttr("系统配置", "数据密码"))));
        private static OleDbCommand Cmd = new OleDbCommand();
        public static DataTable ExcuteTable(string sql)
        {
            try
            {
                Cmd.Connection = Conn;
                Cmd.CommandText = sql;
                OleDbDataAdapter Sda = new OleDbDataAdapter(Cmd);
                DataTable Dt = new DataTable();
                Sda.Fill(Dt);
                return Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("查询Access数据异常。#{0}", err.Message));
                return null;
            }
        }

        public static int ExcuteSql(string Sql)
        {
            try
            {

                Cmd.Connection = Conn;
                Cmd.CommandText = Sql;
                Cmd.Connection.Open();
                return Cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("执行Access命令失败。#{0}", err.Message));
                return -1;
            }
            finally
            {
                Cmd.Connection.Close();
            }
        }
    }
}
